package class1.s2022350143028.data;

import base.data.GoodsBaseData;
import base.model.GoodsBaseModel;
import class1.s2022350143028.model.GoodsModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品信息数据存放，使用集合进行存储数据
 *
 * @author Kcjuly
 * @date 2024-04-25
 *
 */
public class GoodsData extends GoodsBaseData {
    public ArrayList<GoodsModel> goodsDataList;

    public GoodsData(){
        goodsDataList =new ArrayList<>();
        GoodsModel model = new GoodsModel();
        model.setGoodId("G1001");
        model.setGoodName("曲奇饼干");
        model.setSpecifications("中");
        model.setPrice(55);
        model.setinventory(10);
        goodsDataList.add(model);

    }

    //查询商品列表  模糊搜索
    public boolean Get(String query){
        boolean flag=false;
        for (int i = 0; i < goodsDataList.size(); i++){
            GoodsModel good = goodsDataList.get(i);
            if(good.getGoodName().contains(query)){
                System.out.format("%10s %20s %5s %4d %7d\n",good.getGoodId(),good.getGoodName(),good.getSpecifications(),good.getPrice(),good.getInventory());
                flag=true;
            }
        }
        if(flag)return true;
        else return false;

    }

    //录入商品
    public boolean save(GoodsModel goodsBaseModel){
        goodsDataList.add(goodsBaseModel);
        return true;
    }

    //商品删除
    public boolean delete(Object primaryKey){
        String.valueOf(primaryKey);
        boolean flag=false;
        for (int i = 0; i < goodsDataList.size(); i++){
            GoodsModel good = goodsDataList.get(i);
            if(primaryKey.equals(good.getGoodId())){
                this.goodsDataList.remove(good);
                flag=true;
                return true;
            }
        }
        if(!flag){
            System.out.println("没有该商品！");
        }


        return flag;
    }

        /**
     * 编辑商品
     * @param goodsBaseModel 要保存的数据模型
     * @return boolean
     */
        //根据商品编号编辑商品
    public boolean Update(GoodsModel goodsBaseModel){
        boolean flag=false;
        for (int i = 0; i < goodsDataList.size(); i++){
            GoodsModel good = goodsDataList.get(i);
            if(goodsBaseModel.getGoodId().equals(good.getGoodId())){
                good.setGoodName(goodsBaseModel.getGoodName());
                good.setSpecifications(goodsBaseModel.getSpecifications());
                good.setPrice(goodsBaseModel.getPrice());
                good.setinventory(goodsBaseModel.getInventory());
                flag= true;
            }
        }
        return flag;
    }


    //编辑商品库存
    public boolean SetInventory(String goodsId,int number){
        boolean flag=false;
        for (int i = 0; i < goodsDataList.size(); i++){
            GoodsModel good = goodsDataList.get(i);
            if(good.getGoodId().equals(goodsId)){
                int x=good.getInventory();
                x+=number;
                good.setinventory(x);
                System.out.format("%10s %20s %5s %4d %7d\n",good.getGoodId(),good.getGoodName(),good.getSpecifications(),good.getPrice(),good.getInventory());
                flag=true;
                break;
            }
        }
        if(flag)return true;
        else return false;
    }

    @Override
    public List<GoodsBaseModel> get(Object query) {
        return null;
    }

    @Override
    public boolean save(GoodsBaseModel goodsBaseModel) {
        return false;
    }

    @Override
    public boolean update(GoodsBaseModel goodsBaseModel) {
        return false;
    }


}
